#include "JSValueWrapper.h"
#include "WebViewWrapper.h"
#include "WebViewListenerWrapper.h"

using namespace AwesomiumDotNet;

WebViewListenerWrapper::WebViewListenerWrapper(AwesomiumDotNet::WebView^ webView)
{
	_webView = webView;
}

void WebViewListenerWrapper::onBeginLoading(Awesomium::WebView* caller, const std::string& url, const std::wstring& frameName,
	int statusCode, const std::wstring& mimeType)
{
	BeginLoadingEventArgs^ args = gcnew BeginLoadingEventArgs(_webView,
		gcnew String(url.c_str()),
		gcnew String(frameName.c_str()),
		statusCode,
		gcnew String(mimeType.c_str())
		);
	_webView->OnBeginLoading(args);
}

void WebViewListenerWrapper::onBeginNavigation(Awesomium::WebView* caller, const std::string& url, const std::wstring& frameName)
{
	BeginNavigationEventArgs^ args = gcnew BeginNavigationEventArgs(_webView,
		gcnew String(url.c_str()),
		gcnew String(frameName.c_str())
		);
	_webView->OnBeginNavigation(args);
}

void WebViewListenerWrapper::onCallback(Awesomium::WebView* caller, const std::wstring& objectName, const std::wstring& callbackName,
	const Awesomium::JSArguments& args)
{
	Collection<JSValue^>^ out_args = gcnew Collection<JSValue^>();
	for each (Awesomium::JSValue value in args)
	{
		out_args->Add(gcnew JSValue(value));
	}
	CallbackEventArgs^ cb_args = gcnew CallbackEventArgs(_webView,
		gcnew String(objectName.c_str()),
		gcnew String(callbackName.c_str()),
		out_args
		);
	_webView->OnCallback(cb_args);
}

void WebViewListenerWrapper::onChangeCursor(Awesomium::WebView* caller, Awesomium::CursorType cursor)
{
	_webView->OnChangeCursor(gcnew ChangeCursorEventArgs(_webView, (AwesomiumDotNet::CursorType)cursor));
}

void WebViewListenerWrapper::onChangeKeyboardFocus(Awesomium::WebView* caller, bool isFocused)
{
	_webView->OnChangeKeyboardFocus(gcnew ChangeKeyboardFocusEventArgs(_webView, isFocused));
}

void WebViewListenerWrapper::onChangeTargetURL(Awesomium::WebView* caller, const std::string& url)
{
	_webView->OnChangeTargetUrl(gcnew ChangeTargetUrlEventArgs(_webView, gcnew String(url.c_str())));
}

void WebViewListenerWrapper::onChangeTooltip (Awesomium::WebView* caller, const std::wstring& tooltip)
{
	_webView->OnChangeTooltip(gcnew ChangeTooltipEventArgs(_webView, gcnew String(tooltip.c_str())));
}

void WebViewListenerWrapper::onDOMReady(Awesomium::WebView* caller)
{
	_webView->OnDomReady(gcnew DomReadyEventArgs(_webView));
}

void WebViewListenerWrapper::onFinishLoading (Awesomium::WebView* caller)
{
	_webView->OnFinishLoading(gcnew FinishLoadingEventArgs(_webView));
}

void WebViewListenerWrapper::onGetPageContents(Awesomium::WebView* caller, const std::string& url, const std::wstring& contents)
{
	GetPageContentsEventArgs^ args = gcnew GetPageContentsEventArgs(_webView,
		gcnew String(url.c_str()),
		gcnew String(contents.c_str())
		);
	_webView->OnGetPageContents(args);
}

void WebViewListenerWrapper::onOpenExternalLink(Awesomium::WebView* caller, const std::string& url, const std::wstring& source)
{
	OpenExternalLinkEventArgs^ args = gcnew OpenExternalLinkEventArgs(_webView,
		gcnew String(url.c_str()),
		gcnew String(source.c_str())
		);
	_webView->OnOpenExternalLink(args);
}

void WebViewListenerWrapper::onPluginCrashed(Awesomium::WebView* caller, const std::wstring& pluginName)
{
	_webView->OnPluginCrashed(gcnew PluginCrashedEventArgs(_webView, gcnew String(pluginName.c_str())));
}

void WebViewListenerWrapper::onReceiveTitle (Awesomium::WebView* caller, const std::wstring& title, const std::wstring& frameName)
{
	ReceiveTitleEventArgs^ args = gcnew ReceiveTitleEventArgs(_webView,
		gcnew String(title.c_str()),
		gcnew String(frameName.c_str()) );
	_webView->OnReceiveTitle(args);
}

void WebViewListenerWrapper::onRequestDownload(Awesomium::WebView* caller, const std::string& url)
{
	RequestDownloadEventArgs^ args = gcnew RequestDownloadEventArgs(_webView,
		gcnew String(url.c_str())
		);
	_webView->OnRequestDownload(args);
}

void WebViewListenerWrapper::onRequestMove(Awesomium::WebView* caller, int x, int y)
{
	_webView->OnRequestMove(gcnew RequestMoveEventArgs(_webView, x, y));
}

void WebViewListenerWrapper::onWebViewCrashed(Awesomium::WebView* caller)
{
	_webView->OnWebViewCrashed(gcnew WebViewCrashedEventArgs(_webView));
}

void WebViewListenerWrapper::onGetScrollData(Awesomium::WebView* caller, int contentWidth, int contentHeight,
	int preferredWidth, int scrollX, int scrollY)
{
	_webView->OnGetScrollData(gcnew GetScrollDataEventArgs(_webView, contentWidth, contentHeight,
		preferredWidth, scrollX, scrollY));
}

void WebViewListenerWrapper::onRequestFileChooser(Awesomium::WebView* caller, bool selectMultipleFiles,
	const std::wstring& title, const std::wstring& defaultPath)
{
	_webView->OnRequestFileChooser(gcnew RequestFileChooserEventArgs(_webView, selectMultipleFiles,
		gcnew String(title.c_str()),
		gcnew String(defaultPath.c_str())
		));
}

void WebViewListenerWrapper::onJavascriptConsoleMessage(Awesomium::WebView* caller, const std::wstring& message,
	int lineNumber, const std::wstring& source)
{
	_webView->OnJavascriptConsoleMessage(gcnew JavascriptConsoleMessageEventArgs(_webView,
		gcnew String(message.c_str()), lineNumber, gcnew String(source.c_str())));
}
